package com.ssm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.ssm.model.GradeModel;
import com.ssm.model.LoginInfo;
import com.ssm.model.MaterialTemplateModel;
import com.ssm.model.ProfessorModel;
import com.ssm.model.TeacherModel;
import com.ssm.service.MaterialService;
import com.ssm.service.ProfessorService;
import com.ssm.service.TeacherService;

@Controller
@RequestMapping("/admin")
public class AdminController {

	@Autowired
	private TeacherService teacherService;
	@Autowired
	private ProfessorService professorService;
	@Autowired
	private MaterialService materialService;

	/**
	 * 管理员首页展示
	 * @param model 封装登录用户数据
	 * @return 返回首页视图
	 */
	@RequestMapping("/index.do")
	public String showIndex(Model model){
		Subject subject = SecurityUtils.getSubject();
		LoginInfo loginInfo = (LoginInfo)subject.getPrincipal();
		return "admin/index";
	}
	/*教师管理*/

	/**
	 * 教师管理页面
	 * @param pageNum 分页页码，默认1
	 * @param tName 页面搜索按钮 教师姓名
	 * @param tGrade 页面搜索条件 教师专业
	 * @param model 封装教师数据
	 * @return 返回教师管理页面
	 */
	@RequestMapping(value="manageTeacher.do")
	public String selectTeacher(Integer pageNum,String tName,Integer tGrade,Model model){
		if(pageNum==null){
			pageNum = 1;
		}
		if(tName==null || tName.equals("")){
			tName = null;
			model.addAttribute("tName","");
		}else{
			model.addAttribute("tName",tName);
		}
		Page<TeacherModel> teacherModels = teacherService.selectTeacher(pageNum,tName,tGrade); 
		PageInfo<TeacherModel> pageInfo = teacherModels.toPageInfo();
		List<GradeModel> gradeModels = teacherService.selectGrades();
		model.addAttribute("tGrade",tGrade);
		model.addAttribute("pageInfo",pageInfo);
		model.addAttribute("gradeModels",gradeModels);
		return "admin/showTeacher";
	}

	/**
	 * 跳转增加页面
	 * @return 跳转增加人员页面
	 */
	@RequestMapping(value="/add.do",method=RequestMethod.GET)
	public String showAdd(){
		return "admin/addPage";
	}

	/**
	 * 执行增加功能，只增加登录信息，其余用户自己完善
	 * @param loginInfo 增加用户登录信息，
	 * @return 重定向到首页
	 */
	@RequestMapping(value="/add.do",method=RequestMethod.POST)
	public Object doAdd(LoginInfo loginInfo){
		System.out.println("loginInfo:>>>>>"+loginInfo);
		teacherService.insertTeacher(loginInfo);
		HashMap<String, Object> map = new HashMap<>();
		map.put("success", true);
		return "redirect:manageTeacher.do";
	}

	/**
	 * 跳转到修改界面
	 * @param id 被修改人id
	 * @param model 获取被修改人登录信息
	 * @return 返回修改界面
	 */
	@RequestMapping(value="/edit.do",method=RequestMethod.GET)
	public String showEdit(Integer id,Model model){
		LoginInfo loginInfo = teacherService.selectById(id);
		model.addAttribute("loginInfo",loginInfo);
		return "admin/editPage";
	}

	/**
	 * 执行修改操作
	 * @param loginInfo 封装被修改人登录信息
	 * @return 重定向到首页
	 */
	@RequestMapping(value="/edit.do",method=RequestMethod.POST)
	public Object doEdit(LoginInfo loginInfo){
		System.out.println("loginInfo:>>>>>"+loginInfo);
		HashMap<String, Object> paramMap = new HashMap<>();
		paramMap.put("username", loginInfo.getUsername());
		paramMap.put("password", loginInfo.getPassword());
		teacherService.updateTeacher(paramMap);
		return "redirect:manageTeacher.do";
	}

	/**
	 * ajax异步删除人员
	 * @param id 被删除人id
	 * @return 重定向到首页
	 */
	@ResponseBody
	@RequestMapping(value="/delete.do")
	public Object DeleteTeacher(Integer id){
		System.out.println(">>>>>删除id为："+id);
		Integer i = teacherService.deleteTeacher(id);
		HashMap<String,Object> map = new HashMap<>();
		if(i>0){
			System.out.println("删除成功。。。");
			map.put("success", true);
		}
		return map;
	}
	
	/*  专家管理   */

	/**
	 * 专家管理页面
	 * @param pageNum 分页页码
	 * @param pName 页面搜索条件 专家姓名
	 * @param model 封装专家信息
	 * @return 返回专家管理界面
	 */
	@RequestMapping(value="manageProfessor.do")
	public String selectProfessor(Integer pageNum,String pName,Model model){
		if(pageNum==null){
			pageNum = 1;
		}
		if(pName==null || pName.equals("")){
			pName = null;
			model.addAttribute("pName","");
		}else{
			model.addAttribute("pName",pName);
		}
		Page<ProfessorModel> professorModels = professorService.selectProfessor(pageNum, pName);
		PageInfo<ProfessorModel> pageInfo = professorModels.toPageInfo();
		model.addAttribute("pageInfo",pageInfo);
		return "admin/showProfessor";
	}
	
	/* 课程管理 */

	/**
	 * 课程管理-教师列表页
	 * @param pageNum 分页页码
	 * @param tGrade 搜索条件-专业
	 * @param model 封装教师信息
	 * @return 返回教师列表页
	 */
	@RequestMapping("/manageCourseSchedule.do")
	public String manageCourseSchedule(Integer pageNum,Integer tGrade,Model model){
		System.out.println("显示课程安排!");
		if(pageNum==null){
			pageNum = 1;
		}
		if(tGrade==null){
			tGrade=0;
		}
		Page<TeacherModel> teacherModels = teacherService.selectTeacher(pageNum,null,tGrade); 
		PageInfo<TeacherModel> pageInfo = teacherModels.toPageInfo();
		List<GradeModel> gradeModels = teacherService.selectGrades();
		model.addAttribute("pageInfo",pageInfo);
		model.addAttribute("gradeModels",gradeModels);
		model.addAttribute("tGrade",tGrade);
		//查询课程表信息
		return "admin/courseManage";
	}

	/**
	 * 课程设置功能
	 * @param tId 指定教师id
	 * @param tName 指定教师姓名
	 * @param tGradeName 指定教师专业名称
	 * @param model 封装课程信息
	 * @return 返回课程显示页面
	 */
	@RequestMapping(value = "/courseSchedule.do")
	public String getCourseContent(Integer tId,String tName,String tGradeName,Model model){
		System.out.println("::::>>>>>"+tId+"::"+tName+"::"+tGradeName);
		model.addAttribute("tId",tId);
		model.addAttribute("tName",tName);
		model.addAttribute("tGradeName",tGradeName);
		//第一节课list
		List<Object> courseList1 = new ArrayList<Object>();
		//第二节课list
		List<Object> courseList2 = new ArrayList<Object>();
		//第三节课list
		List<Object> courseList3 = new ArrayList<Object>();
		//第四节课list
		List<Object> courseList4 = new ArrayList<Object>();
		//第五节课list
		List<Object> courseList5 = new ArrayList<Object>();
		//查询教师课程
		TeacherModel teacherModel = teacherService.selectCourseByTid(tId);
		List list = new ArrayList<>();
		if(teacherModel.getCourseModel()!=null){
			//得到课程安排
			String coursePlan = teacherModel.getCourseModel().getCoursePlan();
			//转换为map格式，提取合适的数据格式
			JSONObject jsonObject = JSONObject.parseObject(coursePlan);
			Map courseMap = (Map)jsonObject;
			list = (List) courseMap.get(tId);
			for (Object object : list) {
				Map dayMap = (Map)object;
				if(dayMap.get("ctime").equals("1")){
					//第一节课
					courseList1.add(dayMap);
				}else if(dayMap.get("ctime").equals("2")){
					//第二节课
					courseList2.add(dayMap);
				}else if(dayMap.get("ctime").equals("3")){
					//第三节课
					courseList3.add(dayMap);
				}else if(dayMap.get("ctime").equals("4")){
					//第四节课
					courseList4.add(dayMap);
				}else if(dayMap.get("ctime").equals("5")){
					//第五节课
					courseList5.add(dayMap);
				}
			}
		}
		model.addAttribute("courseList1",courseList1);
		model.addAttribute("courseList2",courseList2);
		model.addAttribute("courseList3",courseList3);
		model.addAttribute("courseList4",courseList4);
		model.addAttribute("courseList5",courseList5);
		return "admin/courseSchedule";
	}

	/**
	 * ajax异步动态设置显示课程信息
	 * @param courseData 设置的课程日期，周几的第几节课
	 * @param tId 课程所属教师id
	 * @return 返回设置结果json
	 */
	@ResponseBody
	@RequestMapping(value = "/coursePlan.do",method = RequestMethod.POST)
	public Object planCourse(String courseData,Integer tId){
		JSONObject jsonObject = JSONObject.parseObject(courseData);
		Map courseMap = (Map)jsonObject;
		//根据当前教师id查询课程
		Integer courseNum = teacherService.checkCourseByTid(tId);
		if(courseNum==0){
			//执行插入操作
			teacherService.insertCourse(courseData, tId);
			System.out.println("课程添加成功...");
		}else{
			//执行更新操作
			teacherService.updateCourse(courseData, tId);
			System.out.println("课程更新成功...");
		}
		//更新完课程后将课程id关联到teacher表中的course_id
		HashMap resultMap = new HashMap<>();
		resultMap.put("success", true);
		return resultMap;
	}
	
	
	/* 评估材料项管理 */
	/**
	 * 评估材料项管理界面
	 * @param model 封装评估材料项list集合
	 * @return 返回材料项显示页面
	 */
	@RequestMapping("/manageMaterial.do")
	public String showMaterial(Model model){
		List dataList = materialService.selectMaterialTemplate();
		model.addAttribute("dataList",dataList);
		return "admin/materialTemplate";
	}

	/**
	 * 添加或修改材料项
	 * @param materialName 材料项名称
	 * @param materialRemark 材料项描述，方便后期教师端显示，默认设置为空
	 * @return 重定向到材料项显示页面
	 */
	@RequestMapping("/updateMaterialTemplate.do")
	public String addMaterialTemplate(String materialName,String materialRemark){
		HashMap<String, Object> newTemplateMap = new HashMap<>();
		newTemplateMap.put("k", materialName);
		newTemplateMap.put("v", materialRemark);
		List templateList = materialService.selectMaterialTemplate();
		boolean flag = false;//标识是添加还是更新，，false的话是添加，true的话是更新
		for (Object object : templateList) {
			Map eachMap = (Map)object;
			if(eachMap.get("k").equals(materialName)){//如果之前有数据，替换remark，执行更新材料项操作
				eachMap.replace("v", materialRemark);
				flag=true;
				break;
			}
		}
		if(flag==false){
			templateList.add(newTemplateMap);			
		}
		//将新数据插入到list中,将list更新到数据库中
		materialService.updateMaterialTemplate(templateList);
		return "redirect:manageMaterial.do";
	}

	/**
	 * ajax删除某一材料项
	 * @param materialName 要删除的材料项名称
	 * @param materialRemark 要删除的材料项描述
	 * @return 返回操作结果json
	 */
	@ResponseBody
	@RequestMapping("/deleteMaterialTemplate.do")
	public Object deleteMaterialTemplate(String materialName,String materialRemark){
		Map<String,Object> targetMap = new HashMap<>();
		targetMap.put("k", materialName);
		targetMap.put("v", materialRemark);
		//将需要删除的map放入一个新集合中
		List<Object> targetList = new ArrayList<Object>();
		targetList.add(targetMap);
		System.out.println("targetList>>>>"+targetList);
		//得到原集合数据
		List<Object> templateList = materialService.selectMaterialTemplate();
		//调用原集合的removeAll方法通过循环比较的方法删除自身和targetList中元素相同的元素
		templateList.removeAll(targetList);
		System.out.println("templateList>>>>>"+templateList);
		//将最终数据写入数据库中
		materialService.updateMaterialTemplate(templateList);
		HashMap returnMap = new HashMap<>();
		returnMap.put("success", true);
		return returnMap;
	}
	
	/*评估安排*/
	/**
	 * 评估安排管理
	 * @param pageNum 分页页码
	 * @param tGrade 搜索条件 教师专业
	 * @param model 封装列表数据-教师信息
	 * @return 返回页面显示
	 */
	@RequestMapping("/manageAssessPlan.do")
	public String showAssessPlan(Integer pageNum,Integer tGrade,Model model){
		if(pageNum==null){
			pageNum = 1;
		}
		if(tGrade==null){
			tGrade=0;
		}
		Page<TeacherModel> teacherModels = teacherService.selectTeacher(pageNum,null,tGrade); 
		PageInfo<TeacherModel> pageInfo = teacherModels.toPageInfo();
		List<GradeModel> gradeModels = teacherService.selectGrades();
		model.addAttribute("pageInfo",pageInfo);
		model.addAttribute("gradeModels",gradeModels);
		model.addAttribute("tGrade",tGrade);
		return "admin/assessPlan";
	}
	/*安排评估专家*/
	/**
	 * 安排评估专家
	 * @param model 显示专家列表，封装专家信息
	 * @return 返回专家列表
	 */
	@RequestMapping("/getProfessors.do")
	public String listProfessors(Model model){
		List<ProfessorModel> professorModels = professorService.selectAll();
		model.addAttribute("professors",professorModels);
		return "admin/professorList";
	}
	/*获得专家信息*/
	/**
	 * 获得专家信息
	 * @param pid 指定专家，获得该专家的详细信息
	 * @param tid 教师id，页面用
	 * @param model 封装专家数据
	 * @return 返回专家详细信息页
	 */
	@RequestMapping("/getProfessor.do")
	public String getProfessor(Model model,Integer pid,Integer tid){
		ProfessorModel professorModel = professorService.selectById(pid);
		System.out.println(tid+">>><<<");
		model.addAttribute("professor",professorModel);
		model.addAttribute("tid",tid);
		return "admin/professorInfo";
	}
	/*安排评教*/
	/**
	 *  设定评估安排
	 * @param pid 指定专家id
	 * @param tid  指定教师id
	 * @return 重定向到评估安排教室列表页
	 */
	@RequestMapping("/assessPlan.do")
	public String assessPlan(Integer pid,Integer tid){
		teacherService.insertAssessPlan(tid, pid);
		return "redirect:manageAssessPlan.do";
	}
	/*查看评估进度*/
	/**
	 * 查看评估进度
	 * @param tid  指定教师id
	 * @param model 评估安排信息数据，
	 * @return 返回进度条
	 */
	@RequestMapping("/checkAssessSchedule.do")
	public String showAssessSchedule(Model model,Integer tid){
		HashMap resultMap = teacherService.selectAssessByTid(tid);
		System.out.println("resultMap::::"+resultMap);
		model.addAttribute("resultMap",resultMap);
		return "admin/assessSchedule";
	}
	
}
